package com.itservice.uzalarm;


import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;


public class WeeklySettingDialog extends Dialog implements OnClickListener {
	public static final int RESULT_ALL = 0;
	public static final int RESULT_OK = 1;
    public static final int RESULT_CANCEL = 2;
    public static final int RESULT_ERROR = -1;

    private OnResultListener mOnResultListener = null;
    
    private Button bt1;
	private CheckBox bSun,bMon,bTue,bWed,bThu,bFri,bSat;
	private int weekly = 0;
	private String sWeekly = null;
	
	public WeeklySettingDialog(Context context) {
		super(context);
        setTitle("Weekly Setting");
        setContentView(R.layout.weekly_dialog);
        
        /* Setting View */
        bSun = (CheckBox)findViewById(R.id.weekly_check_sun);
        bSun.setOnClickListener(this);
        bMon = (CheckBox)findViewById(R.id.weekly_check_mon);
        bMon.setOnClickListener(this);
        bTue = (CheckBox)findViewById(R.id.weekly_check_tue);
        bTue.setOnClickListener(this);
        bWed = (CheckBox)findViewById(R.id.weekly_check_wed);
        bWed.setOnClickListener(this);
        bThu = (CheckBox)findViewById(R.id.weekly_check_thu);
        bThu.setOnClickListener(this);
        bFri = (CheckBox)findViewById(R.id.weekly_check_fri);
        bFri.setOnClickListener(this);
        bSat = (CheckBox)findViewById(R.id.weekly_check_sat);
        bSat.setOnClickListener(this);
        bt1 = (Button)findViewById(R.id.setButtonW);
        bt1.setOnClickListener(this);
	}
	
    public interface OnResultListener {
        void OnResult( int resultCode, String WeeklyData,int weekcode );
    }

    public void setOnResultListener(OnResultListener listener) {
        mOnResultListener = listener;
    }

        
    private void createBackData(){
    	String tmp = "";
    	if((weekly & Utility.SUNDAY) == Utility.SUNDAY){
    		tmp += "SUN" + "/";
    	}
    	if((weekly & Utility.MONDAY) == Utility.MONDAY){
    		tmp += "MON" + "/";
    	}
    	if((weekly & Utility.TUESDAY) == Utility.TUESDAY){
    		tmp += "TUE" + "/";
    	}
    	if((weekly & Utility.WEDNESDAY) == Utility.WEDNESDAY){
    		tmp += "WED" + "/";
    	}
    	if((weekly & Utility.THURSDAY) == Utility.THURSDAY){
    		tmp += "THU" + "/";
    	}
    	if((weekly & Utility.FRIDAY) == Utility.FRIDAY){
    		tmp += "FRI" + "/";
    	}
    	if((weekly & Utility.SATURDAY) == Utility.SATURDAY){
    		tmp += "SAT" + "/";
    	}
    	if(tmp.equals("")){
    		tmp = "NON SETTING";
    	}
    	sWeekly = tmp;
    }
    
	@Override
	public void onClick(View v) {
		if(v == bSun){
			if(bSun.isChecked() == true){
				weekly |= Utility.SUNDAY ;
			}else{
				weekly &= ~Utility.SUNDAY;
			}
		}else if(v == bMon){
			if(bMon.isChecked() == true){
				weekly |= Utility.MONDAY ;
			}else{
				weekly &= ~Utility.MONDAY;
			}
		}else if(v == bTue){
			if(bTue.isChecked() == true){
				weekly |= Utility.TUESDAY ;
			}else{
				weekly &= ~Utility.TUESDAY;
			}
		}else if(v == bWed){
			if(bWed.isChecked() == true){
				weekly |= Utility.WEDNESDAY ;
			}else{
				weekly &= ~Utility.WEDNESDAY;
			}
		}else if(v == bThu){
			if(bThu.isChecked() == true){
				weekly |= Utility.THURSDAY ;
			}else{
				weekly &= ~Utility.THURSDAY;
			}
		}else if(v == bFri){
			if(bFri.isChecked() == true){
				weekly |= Utility.FRIDAY ;
			}else{
				weekly &= ~Utility.FRIDAY;
			}
		}else if(v == bSat){
			if(bSat.isChecked() == true){
				weekly |= Utility.SATURDAY;
			}else{
				weekly &= ~Utility.SATURDAY;
			}
		}
		/* Set Button */
		else if(v == bt1){
        	createBackData();
        	mOnResultListener.OnResult(RESULT_OK, sWeekly, weekly);
            dismiss();
		}
	}
}
